package com.thientan.model

import com.thientan.model.core.AuditingInfo
import com.thientan.model.enums.TrangThaiHopDong
import com.thientan.utils.Constants

/**
 * Domain class used to represent entity from table HOP_DONG
 * @author DINH HONG CHAU
 *
 */
class HopDong {
	static embedded = ['auditingInfo']
	AuditingInfo auditingInfo

	static constraints = {
		auditingInfo(nullable: true)
		maHd(unique: true, maxSize: 10)
		soHd(nullable: true)
		nvDoiTruong(nullable: true)
		nvThuNgan(nullable: true)
		dotBan(nullable: true)
		daGiaoPhieu(nullable: true)
		liDoTraHang(nullable: true, maxSize: 100)
		liDoGiamGia(nullable: true, maxSize: 100)
	}

	static mapping = {
		table 'HOP_DONG'
		id generator : 'increment'
		prefixMaHd formula : "substring(ma_hop_dong, 1, 3)"
		columns {
			id column : 'HOP_DONG_ID'
			maHd column : 'MA_HOP_DONG'
			soHd column : 'SO_HOP_DONG'
			nvDoiTruong column: 'DOI_TRUONG_ID'
			nvBan1 column: 'NHAN_VIEN1_ID'
			nvBan2 column: 'NHAN_VIEN2_ID'
			nvThuNgan column: 'NV_THU_NGAN_ID'
			khachHang column: 'KHACH_HANG_ID'
			daGiaoPhieu column: 'NGAY_GIAO_PHIEU'
		}
	}

	static transients = ["trangThaiText", "ngayBanText", "ngayCapNhatText", "maDt", "maNv1", "maNv2", "maTn",
						 "khHoTen", "khDt", "khDchi", "khPxa", "khQhuyen", "khTpho", "maNvCapNhat", "ngayCapNhat"]
	static hasMany = [chiTietThuChis : ChiTietThuChi]

	Set<ChiTietThuChi> chiTietThuChis = []
	String maHd
	Long soHd
	KhachHang khachHang
	NhanVien nvBan1
	NhanVien nvBan2
	NhanVien nvDoiTruong
	NhanVien nvThuNgan
	TrangThaiHopDong trangThai = TrangThaiHopDong.DANG_TRA_GOP
	Double giaTri
	Double tienTraTruoc
	Double tienDaThu
	Double tienConLai
	String liDoTraHang
	Date ngayBan
	Double tienGiamGia = 0
	String liDoGiamGia
	Integer dotBan
	Date daGiaoPhieu

	// Transient properties and getters
	String prefixMaHd
	String maDt
	String maNv1
	String maNv2
	String maTn
	String khHoTen
	String khDt
	String khDchi
	String khPxa
	String khQhuyen
	String khTpho
	String maNvCapNhat
	Date ngayCapNhat
	
	String getTrangThaiText() {
		return trangThai.displayText
	}
	
	String getNgayCapNhatText() {
		new java.text.SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT).format(ngayCapNhat)
	}

	String getNgayBanText() {
		new java.text.SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT).format(ngayBan)
	}

	// Internal processes
	int updateTraGop(Double tienThuThem, Date ngayBan = null, String ghiChu = null) {
		if (tienThuThem > tienConLai) {
			return 1
		}
		tienDaThu += tienThuThem
		tienConLai -= tienThuThem
		daGiaoPhieu = null
		
		if (tienThuThem < 0 && trangThai == TrangThaiHopDong.KET_THUC) {
			trangThai = TrangThaiHopDong.DANG_TRA_GOP
		}
		
		if (tienConLai == 0) {
			trangThai = TrangThaiHopDong.KET_THUC
		}
		
		// Only create a new ChiTietThuChi, if tienThuThem != 0.
		if(tienThuThem != 0){
			ChiTietThuChi cttc = new ChiTietThuChi([
						"nhanVien.id" : nvThuNgan.id,
						"soTien" : tienThuThem,
						"ngayLap" : ngayBan ?: new Date(),
						"ghiChu" : ghiChu
					])
			addToChiTietThuChis(cttc)
		}
		return 0
	}

	void updateDenTraHang(TrangThaiHopDong trangThaiMoi) {
		trangThai = trangThaiMoi
		tienConLai = 0
		daGiaoPhieu = null
	}
}
